package com.kermel.andromeda.data;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import com.kermel.common.util.StringUtils;

public class Alarm implements Parcelable {
    public String name = StringUtils.EMPTY;
    public boolean enabled = true;
    public boolean vibrate = true;
    public long triggerTime;
    public String soundFilename;
    public UUID uuid;
    public long rowID;

    public Alarm() {
        triggerTime = initializeTriggerTime();
        uuid = UUID.randomUUID();
    }
    
    public long initializeTriggerTime() {
        long alarmTriggerTime = 0;
        
        Calendar rightNow = Calendar.getInstance();
        alarmTriggerTime = rightNow.getTimeInMillis();
        
        return alarmTriggerTime;
    }
    
    @Override
    public String toString() {
        StringBuffer result = new StringBuffer();
        
        result.append("\n    rowID: ").append(rowID);
        result.append("\n    name: ").append(name);
        result.append("\n    enabled: ").append(enabled);
        result.append("\n    vibrate: ").append(vibrate);
        result.append("\n    triggerTime: ").append(triggerTime);
        result.append(" [").append(Alarm.triggerTimeAsDateString(triggerTime)).append("]");
        result.append("\n    soundFilename: ").append(soundFilename);
        result.append("\n    uuid: ").append(uuid.toString());
        result.append("\n");
        
        return result.toString();
    }
    
    public static String triggerTimeAsDateString(long triggerTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); //"hh:mm aa"
        Date date = new Date(triggerTime);
        return formatter.format(date);
    }
    
    public UUID getUUID() {
        return uuid;
    }
    
    public int describeContents() {
        return 0;
    }

    public static final String PARCEL_NAME = "name";
    public static final String PARCEL_SOUND_FILENAME = "soundFilename";
    public static final String PARCEL_ENABLED = "enabled";
    public static final String PARCEL_VIBRATE = "vibrate";
    public static final String PARCEL_TRIGGER_TIME = "triggerTime";
    public static final String PARCEL_ID = "id";
    
    public Alarm(Parcel source) {
        Bundle bundle = source.readBundle();
        if (bundle != null) {
            name = bundle.getString(PARCEL_NAME);
            soundFilename = bundle.getString(PARCEL_SOUND_FILENAME);
            enabled = bundle.getBoolean(PARCEL_ENABLED);
            vibrate = bundle.getBoolean(PARCEL_VIBRATE);
            triggerTime = bundle.getLong(PARCEL_TRIGGER_TIME);
            String uuidString = bundle.getString(PARCEL_ID);
            if (uuidString != null) {
                uuid = UUID.fromString(uuidString);
            }
        }
    }
    
    public void writeToParcel(Parcel dest, int flags) {
        Bundle bundle = new Bundle();
        
        bundle.putString(PARCEL_NAME, name);
        bundle.putString(PARCEL_SOUND_FILENAME, soundFilename);
        bundle.putBoolean(PARCEL_ENABLED, enabled);
        bundle.putBoolean(PARCEL_VIBRATE, vibrate);
        bundle.putLong(PARCEL_TRIGGER_TIME, triggerTime);
        bundle.putString(PARCEL_ID, uuid.toString());
        
        dest.writeBundle(bundle);
    }
    
    public static final Parcelable.Creator<Alarm> CREATOR = new Parcelable.Creator<Alarm>() {
        public Alarm createFromParcel(Parcel source) {
            return new Alarm(source);
        }
        
        public Alarm[] newArray(int size) {
            return new Alarm[size];
        }
    };
        
}
